/*
 * =====================================================================================
 *
 *       Filename:  Fsast_Socket.cpp
 *
 *    Description: 跨平台的客服端和服务端的封装
 *
 *        Version:  1.0
 *        Created:  2025年03月25日 21时20分26秒
 *       Revision:  none
 *       Compiler:  g++
 *
 *         Author:  YANGYE, 
 *   Organization:  
 *
 * =====================================================================================
 */
#include "Fsast_Socket.hpp"


namespace FS{
	Fsast_Socket* Fsast_Socket::SocketIpv4 = nullptr;
	Fsast_Socket* Fsast_Socket::SocketIpv6 = nullptr;
	SYSTEM Fsast_Socket::system = DEFAULT;
	//服务端和客户端获取单例函数
	Fsast_Socket* Server_Socket::GetServerSocketIpv4(int port){
		if(Fsast_Socket::SocketIpv4 == nullptr && (system == DEFAULT || system == SERVER)){
			system = SERVER;
			return new Server_Socket(port,IPV4);//ipv4是临时的不能传引用	
		}
		return Fsast_Socket::SocketIpv4;	
	}
	Fsast_Socket* Server_Socket::GetServerSocketIpv6(int port) {
		if (Fsast_Socket::SocketIpv6 == nullptr && (system == DEFAULT || system == SERVER)) {
			system = SERVER;
			return new Server_Socket(port, IPV6);
		}
		return Fsast_Socket::SocketIpv6;
	}
	Fsast_Socket* Client_Socket::GetClientSocketIpv4(std::string address, int port) {
		if (Fsast_Socket::SocketIpv4 == nullptr && (system == DEFAULT || system == CLIENT)) {
			return new Client_Socket(address, port,IPV4);
		}
		return Fsast_Socket::SocketIpv4;
	}
	Fsast_Socket* Client_Socket::GetClientSocketIpv6(std::string address, int port) {
		if (Fsast_Socket::SocketIpv6 == nullptr && (system == DEFAULT || system == CLIENT)) {
			return new Client_Socket(address, port,IPV6);
		}
		return Fsast_Socket::SocketIpv6;
	}
	//构造函数
	Server_Socket::Server_Socket(int port,IP_MODEL ipv) :Fsast_Socket(port,ipv) {
		if (!Initialize()) {
			std::cerr << "The server socket creation and initialization failed!" << std::endl;
			Fsast_Socket::deletable();
		}
	}
	Client_Socket::Client_Socket(std::string address, int port, IP_MODEL ipv):Fsast_Socket(port,ipv) {
		Client_Socket::address = address;
		if (!Initialize()) {
			std::cerr << "The client socket creation and initialization failed!" << std::endl;
			Fsast_Socket::deletable();
		}
	}
	Fsast_Socket::Fsast_Socket(int port,IP_MODEL ipv) : socket_ipv(ipv),port(port) {
	}
	//初始化函数
	bool Server_Socket::Initialize(/*int& port*/) {
		//windows需要初始化
		#ifndef WIN32
			WSADATA wsaData;
			if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0){
				return false;
			}
		#endif
		if (socket_ipv == IPV4) {//-----------------------------------ipv4-----------------------------
			//创建套接字
			server_socketIpv4 = socket(AF_INET, SOCK_STREAM, 0);
			if (server_socketIpv4 == -1) {
				return false;
			}
			//设置地址族
			sockaddr_in server_addr;
			memset(&server_addr, 0, sizeof(server_addr));//初始化为0
			server_addr.sin_family = AF_INET;
			server_addr.sin_addr.S_un.S_addr = INADDR_ANY;
			server_addr.sin_port = htons(port);
			//绑定server套接字
			if (bind(server_socketIpv4, reinterpret_cast<sockaddr*>(&server_addr), sizeof(server_addr)) == -1) {
				return false;
			}
			//监听连接
			if (listen(server_socketIpv4, 5) == -1) {
				return false;
			}
		}
		else {//-----------------------------------------------ipv6------------------------------------
			//创建套接字
			server_socketIpv6 = socket(AF_INET6, SOCK_STREAM, 0);
			if (server_socketIpv6 == -1) {
				return false;
			}
			//设置地址族
			sockaddr_in6 server_addr;
			memset(&server_addr, 0, sizeof(server_addr));//初始化为0
			server_addr.sin6_family = AF_INET6;
			server_addr.sin6_addr = in6addr_any;
			server_addr.sin6_port = htons(port);
			//绑定server套接字
			if (bind(server_socketIpv6, reinterpret_cast<sockaddr*>(&server_addr), sizeof(server_addr)) == -1) {
				return false;
			}
			//监听连接
			if (listen(server_socketIpv6, 5) == -1) {
				return false;
			}
		}
		return true;
	}
	bool Client_Socket::Initialize() {
		#ifndef WIN32
			WSADATA wsaData;
			if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
				return false;
			}
		#endif
		if (socket_ipv == IPV4) {
				client_socketIpv4 = socket(AF_INET, SOCK_STREAM, 0);
				if (client_socketIpv4 == -1) {
					return false;
				}
				sockaddr_in server_addr;
				memset(&server_addr, 0, sizeof(server_addr));
				server_addr.sin_family = AF_INET;
				server_addr.sin_port = htons(port);
				inet_pton(AF_INET, address.c_str(), &server_addr.sin_addr);
				if (connect(client_socketIpv4, reinterpret_cast<sockaddr*>(&server_addr), sizeof(server_addr)) == -1) {
					return false;
				}
			}
		else if (socket_ipv == IPV6) {
			client_socketIpv6 = socket(AF_INET6, SOCK_STREAM, 0);
			if (client_socketIpv6 == -1) {
				return false;
			}
			sockaddr_in6 server_addr;
			memset(&server_addr, 0, sizeof(server_addr));
			server_addr.sin6_family = AF_INET6;
			server_addr.sin6_port = htons(port);
			inet_pton(AF_INET6, address.c_str(), &server_addr.sin6_addr);
			if (connect(client_socketIpv6, reinterpret_cast<sockaddr*>(&server_addr), sizeof(server_addr)) == -1) {
				return false;
			}
		}
		return true;
	}
	//析构函数
	void Fsast_Socket::deletable() {
		if (socket_ipv == IPV4) {
			delete Fsast_Socket::SocketIpv4;
		}
		else if (socket_ipv == IPV6) {
			delete Fsast_Socket::SocketIpv6;
		}
	}
	Server_Socket::~Server_Socket() {
	#ifndef WIN32
			closesocket(server_socketIpv4);
	#elif __linux__
			close(server_socketIpv4);
	#endif
	}
	Client_Socket::~Client_Socket() {
	#ifdef WIN32
			closesocket(client_socketIpv4);
	#elif __linux__
			close(client_socketIpv4);
	#endif
	}
	//发送函数

	//接收函数
}


int main() {
	using namespace FS;
	Fsast_Socket* socket_IPV4;
	Fsast_Socket* socket_IPV6;
	socket_IPV4 = Server_Socket::GetServerSocketIpv4(80);
	socket_IPV6 = Server_Socket::GetServerSocketIpv6(80);
	a
	

}